﻿using System;
using System.Collections.Generic;
using System.Data;
//using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using PessModel;
using PessMvc.Models;
using PessMvc.Helpers;
using System.Data.Entity;

namespace PessMvc.Controllers
{
    public class ActivityACLController : Controller
    {
        private PESSEntities db = new PESSEntities();

        // GET: /ActivityACL/

        public ActionResult Index(int activityID=-1)
        {
            ACLModel aclModel = new ACLModel();
            var activities = from a in db.Activities
                                   select a;
            if (activityID == -1) 
            {
                if (activities.Count() > 0)
                {
                    activityID = activities.First().ID;
                }
            }
            aclModel.SelectedActivityID = activityID;
            //activities = activities.Where(a => a.ID == activityID);
            aclModel.ACLMembers = db.ActivityACLs.Include("Author").Where(a => a.ActivityID == activityID).ToList().Select(a => a.Author.ConvertToAuthorModel()).ToList();
            List<SelectListItem> activityList = (from a in activities
                                                   select new { a.Name, a.ID }).ToList().Select(a=>new SelectListItem{Text=a.Name,Value=a.ID.ToString()}).ToList();
            aclModel.ActivitySelectList= new SelectList(activityList, "Value", "Text",activityID==-1?Convert.ToInt32(activityList.First().Value):activityID);//, selectedOption);        
            return View(aclModel);
        }
        
        [HttpPost]
        public ActionResult ChooseACLMembers(int activityID)
        {
            var authors = (from a in db.Authors
                           where !db.ActivityACLs.Any(m => m.AuthorSN == a.SerialNumber && m.ActivityID==activityID)
                           select a).ToList().Select(a => a.ConvertToAuthorModel());
            ViewBag.ActivityID = activityID;
            return View(authors);
        }
        [HttpPost]
        public ActionResult ChooseACLMembersConfirmed(int activityID)
        {
            if (Request["AuthorSN"] != null)
            {
                string[] selectedAuthorSNList = Request["AuthorSN"].Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
                foreach (var sn in selectedAuthorSNList)
                {
                    var author = db.Authors.Find(sn);
                    if (db.ActivityACLs.Where(a => a.AuthorSN == sn&&a.ActivityID==activityID).Count() == 0)//Author is not a member of ACL
                    {
                        db.ActivityACLs.Add(new ActivityACL() { ActivityID=activityID,AuthorSN=sn });
                    }
                }
                db.SaveChanges();
            }
            return RedirectToAction("Index", "ActivityACL", new { activityID = activityID });
        }

        public ActionResult RemoveACLMembers(int activityID)
        {
            if (Request["AuthorSN"] != null)
            {
                string[] selectedAuthorSNList = Request["AuthorSN"].Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
                foreach (var sn in selectedAuthorSNList)
                {
                    ActivityACL aclmember = db.ActivityACLs.Where(a => a.AuthorSN == sn && a.ActivityID == activityID).FirstOrDefault();
                    if (aclmember != null)
                    {
                        db.ActivityACLs.Remove(aclmember);
                    }
                }
                db.SaveChanges();
            }
            return RedirectToAction("Index", "ActivityACL", new { activityID = activityID });
        }
        //
        // GET: /ActivityACL/Details/5

        public ActionResult Details(string id = null)
        {
            ActivityACL activityacl = db.ActivityACLs.Find(id);
            if (activityacl == null)
            {
                return HttpNotFound();
            }
            return View(activityacl);
        }

        //
        // GET: /ActivityACL/Create

        public ActionResult Create()
        {
            ViewBag.ActivityID = new SelectList(db.Activities, "ID", "Name");
            ViewBag.AuthorSN = new SelectList(db.Authors, "SerialNumber", "RealName");
            return View();
        }

        //
        // POST: /ActivityACL/Create

        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Create(ActivityACL activityacl)
        {
            if (ModelState.IsValid)
            {
                db.ActivityACLs.Add(activityacl);
                db.SaveChanges();
                return RedirectToAction("Index");
            }

            ViewBag.ActivityID = new SelectList(db.Activities, "ID", "Name", activityacl.ActivityID);
            ViewBag.AuthorSN = new SelectList(db.Authors, "SerialNumber", "RealName", activityacl.AuthorSN);
            return View(activityacl);
        }

        //
        // GET: /ActivityACL/Edit/5

        public ActionResult Edit(string id = null)
        {
            ActivityACL activityacl = db.ActivityACLs.Find(id);
            if (activityacl == null)
            {
                return HttpNotFound();
            }
            ViewBag.ActivityID = new SelectList(db.Activities, "ID", "Name", activityacl.ActivityID);
            ViewBag.AuthorSN = new SelectList(db.Authors, "SerialNumber", "RealName", activityacl.AuthorSN);
            return View(activityacl);
        }

        //
        // POST: /ActivityACL/Edit/5

        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Edit(ActivityACL activityacl)
        {
            if (ModelState.IsValid)
            {
                db.Entry(activityacl).State = System.Data.Entity.EntityState.Modified;
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            ViewBag.ActivityID = new SelectList(db.Activities, "ID", "Name", activityacl.ActivityID);
            ViewBag.AuthorSN = new SelectList(db.Authors, "SerialNumber", "RealName", activityacl.AuthorSN);
            return View(activityacl);
        }

        //
        // GET: /ActivityACL/Delete/5

        public ActionResult Delete(string id = null)
        {
            ActivityACL activityacl = db.ActivityACLs.Find(id);
            if (activityacl == null)
            {
                return HttpNotFound();
            }
            return View(activityacl);
        }

        //
        // POST: /ActivityACL/Delete/5

        [HttpPost, ActionName("Delete")]
        [ValidateAntiForgeryToken]
        public ActionResult DeleteConfirmed(string id)
        {
            ActivityACL activityacl = db.ActivityACLs.Find(id);
            db.ActivityACLs.Remove(activityacl);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
    }
}